﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
#if !NETCOREAPP
using System.Web;
using System.Web.Mvc;
using System.Web.WebPages;
    using System.Web.Mvc.Html;

#endif

#if NETCOREAPP
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.Mvc.Rendering;
using Kugar.Core.Web;
#endif
using Kugar.Core.ExtMethod;
using Kugar.MVC.MobileUI.WeUI.Helpers;

namespace Kugar.MVC.MobileUI.WeUI.PageControls.PageCommons
{
    public class PageCommon_ListQueryBox:ContainerBase
    {
        public PageCommon_ListQueryBox(ViewContext viewContext) : base(viewContext)
        {
            this.Css("weui-querybox weui-querybox_3");
        }

        /// <summary>
        /// 添加一个文本控件
        /// </summary>
        /// <returns></returns>
        public ListQueryItem_TextBox AddTextBox()
        {
            return new ListQueryItem_TextBox(viewContext:ViewContext);
        }

        /// <summary>
        /// 添加一个值范围控件
        /// </summary>
        /// <returns></returns>
        public ListQueryItem_ValueRange AddValueRange()
        {
            return new ListQueryItem_ValueRange(ViewContext);
        }

        /// <summary>
        /// 添加一个单选列表
        /// </summary>
        /// <returns></returns>
        public ListQueryItem_Radio AddSingleSelect()
        {
            return new ListQueryItem_Radio(ViewContext);
        }

        /// <summary>
        /// 添加一个多选列表
        /// </summary>
        /// <returns></returns>
        public ListQueryItem_Checkbox AddMultiSelect()
        {
            return new ListQueryItem_Checkbox(ViewContext);
        }

        public override void Dispose()
        {
            ViewContext.Writer.WriteLine("</form>");

            ViewContext.Writer.WriteLine("<div class=\"weui-flex weui-querybox-buttonarea\">");
            ViewContext.Writer.WriteLine("<div class=\"weui-flex__item\"><button type=\"button\" class=\"weui-btn weui-btn_warn close-querybox-button\">取消</button></div>");
            ViewContext.Writer.WriteLine("<div class=\"weui-flex__item\"><button type=\"submit\" class=\"weui-btn weui-btn_primary\">确定</button></div>");
            ViewContext.Writer.WriteLine("</div> <!-- end buttonarea -->");

            ViewContext.Writer.WriteLine("</div> <!-- end weui-querybox -->");
        }

        internal override ContainerBase RenderInternal()
        {
            if (_attrs.Attributes.ContainsKey("id"))
            {
                _attrs.ID("qb" + WeUI.NextControlID());
            }

            ViewContext.Writer.WriteLine($"<div {_attrs}>");

            ViewContext.Writer.WriteLine($"<form class=\"weui-querybox-form\">");

            return this;
        }


        public class ListQueryItem_Radio : ControlBase
        {
            private List<SelectorItem> _lst = new List<SelectorItem>(5);
            private string _radioValue = "";
            private string _radioName = "";
            private string _label = "";
            private string _id = "";
            private bool _onchangeSubmit = false;
            private int _displayPerRow = 4;

            public ListQueryItem_Radio(ViewContext viewContext) : base(viewContext)
            {
                this.Css("weui-flex weui-querybox-radio");

            }

            public ListQueryItem_Radio FirstItem(string title, string value)
            {
                _lst.Insert(0, new SelectorItem(title, value));

                return this;
            }

            public ListQueryItem_Radio Options(IEnumerable<SelectorItem> opts)
            {
                _lst.AddRange(opts);

                return this;
            }

            /// <summary>
            /// 单选框的选项,使用一个数据源以及标题获取函数和值获取函数
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="source"></param>
            /// <param name="titleFunc"></param>
            /// <param name="valueFunc"></param>
            /// <returns></returns>
            public ListQueryItem_Radio Options<T>(IEnumerable<T> source, Func<T, string> titleFunc,
                Func<T, string> valueFunc)
            {
                _lst.Clear();

                _lst.AddRange(OptionHelper.Convert(source, titleFunc, valueFunc));

                return this;
            }

            /// <summary>
            /// 单选框的选项,使用一个数据源以及标题获取函数和值获取函数
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="source"></param>
            /// <param name="titleFunc"></param>
            /// <param name="valueFunc"></param>
            /// <returns></returns>
            public ListQueryItem_Radio Options<T>(IEnumerable<T> source, Func<T, HelperResult> titleFunc,
                Func<T, HelperResult> valueFunc)
            {
                _lst.Clear();

                _lst.AddRange(OptionHelper.Convert(source,(data)=>titleFunc(data).ToHtmlString(), (data) => valueFunc(data).ToHtmlString()));

                return this;
            }


            /// <summary>
            /// 单选框的选项,用于enum的显示
            /// </summary>
            /// <param name="enumType"></param>
            /// <returns></returns>
            public ListQueryItem_Radio Options(Type enumType)
            {
                _lst.Clear();

                _lst.AddRange(OptionHelper.Convert(enumType));

                return this;
            }

            /// <summary>
            /// 单选框的name
            /// </summary>
            /// <param name="name"></param>
            /// <returns></returns>
            public ListQueryItem_Radio Name(string name)
            {
                _radioName = name;

                return this;
            }

            /// <summary>
            /// 选择的值
            /// </summary>
            /// <param name="value"></param>
            /// <returns></returns>
            public ListQueryItem_Radio Value(string value)
            {
                _radioValue = value;

                return this;
            }

            /// <summary>
            /// 选择的值
            /// </summary>
            /// <param name="value"></param>
            /// <returns></returns>
            public ListQueryItem_Radio Value(int value)
            {
                _radioValue = value.ToString();

                return this;
            }

            /// <summary>
            /// 标签
            /// </summary>
            /// <param name="label"></param>
            /// <returns></returns>
            public ListQueryItem_Radio Label(string label)
            {
                _label = label;

                return this;
            }

            /// <summary>
            /// 每行显示的选项数量
            /// </summary>
            /// <param name="displayRow"></param>
            /// <returns></returns>
            public ListQueryItem_Radio DisplayPreRow(int displayPerRow)
            {
                if (displayPerRow<=0)
                {
                    throw new ArgumentOutOfRangeException(nameof(displayPerRow));
                }

                _displayPerRow = displayPerRow;

                return this;
            }

            /// <summary>
            /// 当修改选项值时,是否自动提交
            /// </summary>
            /// <param name="autoSubmit"></param>
            /// <returns></returns>
            public ListQueryItem_Radio AutoSubmitOnChange(bool autoSubmit = true)
            {
                _onchangeSubmit = autoSubmit;

                return this;
            }

            public override MvcHtmlString Render()
            {
                ViewContext.Writer.WriteLine($"<div {_attrs} data-display=\"{_displayPerRow}\" data-onchangesubmit=\"{_onchangeSubmit.ToStringEx().ToLower()}\">");
                ViewContext.Writer.WriteLine("<div class=\"weui-flex__item weui-flex-row\">");

                ViewContext.Writer.WriteLine($"<div class=\"weui-cells__title\">");

                ViewContext.Writer.WriteLine($"<span>{_label}</span>");

                if (_lst.Count>_displayPerRow*2) //如果超出2行的数量,则显示一个下拉按钮
                {
                    ViewContext.Writer.WriteLine("<span class=\"btnShowAll\">全部 &and;</span></div>");
                }

                ViewContext.Writer.WriteLine("</div> <!-- end cells__title -->");

                ViewContext.Writer.WriteLine("<div class=\"weui-cells weui-cells__body\">");

                if (string.IsNullOrWhiteSpace(_radioValue))
                {
                    _radioValue = _lst[0].Value;
                }

                for (int i = 0; i < _lst.Count; i+= _displayPerRow)
                {
                    ViewContext.Writer.WriteLine("<div class=\"weui-flex weui-querybox-block\">");

                    for (int j = 0; j < 3; j++)
                    {
                        var item = _lst[i+j];
                        
                        ViewContext.Writer.WriteLine($"<div class=\"weui-flex__item weui-querybox-item\"><input type=\"radio\" {(_radioValue==item.Value).If("checked=\"checked\"")} name=\"{_radioName}\" value=\"{item.Value}\" /><p>{item.Title}</p></div>");
                    }

                    ViewContext.Writer.WriteLine("</div> <!-- end querybox-block -->");
                }

                ViewContext.Writer.WriteLine("</div> <!-- end cells__body -->");
                ViewContext.Writer.WriteLine("</div> <!-- end flex-row -->");
                ViewContext.Writer.WriteLine("</div> <!-- end querybox-radio -->");

                return MvcHtmlString.Empty;
            }
        }

        public class ListQueryItem_Checkbox : ControlBase
        {
            private List<SelectorItem> _lst = new List<SelectorItem>(5);
            private string[] _checkValue =null;
            private string _checkboxName = "";
            private string _label = "";
            private string _id = "";
            private bool _onchangeSubmit = false;
            private int _displayPerRow = 4;

            public ListQueryItem_Checkbox(ViewContext viewContext) : base(viewContext)
            {
                this.Css("weui-flex weui-querybox-check");

            }

            public ListQueryItem_Checkbox FirstItem(string title, string value)
            {
                _lst.Insert(0, new SelectorItem(title, value));

                return this;
            }

            public ListQueryItem_Checkbox Options(IEnumerable<SelectorItem> opts)
            {
                _lst.AddRange(opts);

                return this;
            }

            /// <summary>
            /// 单选框的选项,使用一个数据源以及标题获取函数和值获取函数
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="source"></param>
            /// <param name="titleFunc"></param>
            /// <param name="valueFunc"></param>
            /// <returns></returns>
            public ListQueryItem_Checkbox Options<T>(IEnumerable<T> source, Func<T, string> titleFunc,
                Func<T, string> valueFunc)
            {
                _lst.Clear();

                _lst.AddRange(OptionHelper.Convert(source, titleFunc, valueFunc));

                return this;
            }

            /// <summary>
            /// 单选框的选项,使用一个数据源以及标题获取函数和值获取函数
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="source"></param>
            /// <param name="titleFunc"></param>
            /// <param name="valueFunc"></param>
            /// <returns></returns>
            public ListQueryItem_Checkbox Options<T>(IEnumerable<T> source, Func<T, HelperResult> titleFunc,
                Func<T, HelperResult> valueFunc)
            {
                _lst.Clear();

                _lst.AddRange(OptionHelper.Convert(source, (data) => titleFunc(data).ToHtmlString(), (data) => valueFunc(data).ToHtmlString()));

                return this;
            }


            /// <summary>
            /// 单选框的选项,用于enum的显示
            /// </summary>
            /// <param name="enumType"></param>
            /// <returns></returns>
            public ListQueryItem_Checkbox Options(Type enumType)
            {
                _lst.Clear();

                _lst.AddRange(OptionHelper.Convert(enumType));

                return this;
            }

            /// <summary>
            /// 单选框的name
            /// </summary>
            /// <param name="name"></param>
            /// <returns></returns>
            public ListQueryItem_Checkbox Name(string name)
            {
                _checkboxName = name;

                return this;
            }

            /// <summary>
            /// 选择的值
            /// </summary>
            /// <param name="value"></param>
            /// <returns></returns>
            public ListQueryItem_Checkbox Value(IEnumerable<string> value)
            {
                _checkValue = value.ToArrayEx();

                return this;
            }

            /// <summary>
            /// 选择的值
            /// </summary>
            /// <param name="value"></param>
            /// <returns></returns>
            public ListQueryItem_Checkbox Value(int[] value)
            {
                _checkValue = value.Select(x=>x.ToString()).ToArrayEx();

                return this;
            }

            /// <summary>
            /// 标签
            /// </summary>
            /// <param name="label"></param>
            /// <returns></returns>
            public ListQueryItem_Checkbox Label(string label)
            {
                _label = label;

                return this;
            }

            /// <summary>
            /// 每行显示的选项数量
            /// </summary>
            /// <param name="displayRow"></param>
            /// <returns></returns>
            public ListQueryItem_Checkbox DisplayPreRow(int displayPerRow)
            {
                if (displayPerRow <= 0)
                {
                    throw new ArgumentOutOfRangeException(nameof(displayPerRow));
                }

                _displayPerRow = displayPerRow;

                return this;
            }

            /// <summary>
            /// 当修改选项值时,是否自动提交
            /// </summary>
            /// <param name="autoSubmit"></param>
            /// <returns></returns>
            public ListQueryItem_Checkbox AutoSubmitOnChange(bool autoSubmit = true)
            {
                _onchangeSubmit = autoSubmit;

                return this;
            }

            public override MvcHtmlString Render()
            {
                ViewContext.Writer.WriteLine($"<div {_attrs} data-display=\"{_displayPerRow}\" data-onchangesubmit=\"{_onchangeSubmit.ToStringEx().ToLower()}\">");
                ViewContext.Writer.WriteLine("<div class=\"weui-flex__item weui-flex-row\">");

                ViewContext.Writer.WriteLine($"<div class=\"weui-cells__title\">");

                ViewContext.Writer.WriteLine($"<span>{_label}</span>");

                if (_lst.Count > _displayPerRow * 2) //如果超出2行的数量,则显示一个下拉按钮
                {
                    ViewContext.Writer.WriteLine("<span class=\"btnShowAll\">全部 &and;</span></div>");
                }

                ViewContext.Writer.WriteLine("</div> <!-- end cells__title -->");

                ViewContext.Writer.WriteLine("<div class=\"weui-cells weui-cells__body\">");

                //if (string.IsNullOrWhiteSpace(_radioValue))
                //{
                //    _radioValue = _lst[0].Value;
                //}

                for (int i = 0; i < _lst.Count; i += _displayPerRow)
                {
                    ViewContext.Writer.WriteLine("<div class=\"weui-flex weui-querybox-block\">");

                    for (int j = 0; j < 3; j++)
                    {
                        var item = _lst[i + j];

                        ViewContext.Writer.WriteLine($"<div class=\"weui-flex__item weui-querybox-item\"><input type=\"checkbox\" {(_checkValue.Contains(item.Value)).If("checked=\"checked\"")} name=\"{_checkboxName}\" value=\"{item.Value}\" /><p>{item.Title}</p></div>");
                    }

                    ViewContext.Writer.WriteLine("</div> <!-- end querybox-block -->");
                }

                ViewContext.Writer.WriteLine("</div> <!-- end cells__body -->");
                ViewContext.Writer.WriteLine("</div> <!-- end flex-row -->");
                ViewContext.Writer.WriteLine("</div> <!-- end querybox-radio -->");

                return MvcHtmlString.Empty;
            }
        }

        public class ListQueryItem_TextBox:ControlBase
        {
            private string _placeholder = "";
            private string _value = "";
            private string _name = "";
            private string _label = "";

            public ListQueryItem_TextBox(ViewContext viewContext) : base(viewContext)
            {
            }

            public ListQueryItem_TextBox Label(string label)
            {
                _label = label;

                return this;
            }

            public ListQueryItem_TextBox Placeholder(string text)
            {
                _placeholder = text;

                return this;
            }

            public ListQueryItem_TextBox Name(string name)
            {
                _name = name;

                return this;
            }

            public ListQueryItem_TextBox Value(string value)
            {
                _value = value;

                return this;
            }

            public override MvcHtmlString Render()
            {
                ViewContext.Writer.WriteLine("<div class=\"weui-flex weui-querybox-text\">");
                ViewContext.Writer.WriteLine("<div class=\"weui-flex__item weui-flex-row\">");
                ViewContext.Writer.WriteLine("<div class=\"weui-search-bar__box\">");
                ViewContext.Writer.WriteLine($"<i class=\"weui-icon-search\"></i><input type=\"search\" name=\"{_name}\" class=\"weui-search-bar__input\" placeholder=\"{_placeholder}\" value=\"{_value}\"><a href=\"javascript:\" class=\"weui-icon-clear\"></a>");
                ViewContext.Writer.WriteLine("</div< <!-- end bar__box -->");
                ViewContext.Writer.WriteLine("</div> <!-- end weui-flex__item -->");
                ViewContext.Writer.WriteLine("</div> <!-- end querybox-text -->");

                return MvcHtmlString.Empty;
            }
        }

        public class ListQueryItem_ValueRange : ControlBase
        {
            private string _startName = "";
            private string _endName = "";
            private string _startPlaceholder = "";
            private string _endPlaceholder = "";
            private string _startValue = "";
            private string _endValue = "";
            private string _label = "";

            public ListQueryItem_ValueRange(ViewContext viewContext) : base(viewContext)
            {
                this.Css("weui-flex weui-querybox-value");
            }

            public ListQueryItem_ValueRange Placeholder(string startPlaceholder,string endPlaceholder)
            {
                _startPlaceholder = startPlaceholder;
                _endPlaceholder = endPlaceholder;

                return this;
            }

            public ListQueryItem_ValueRange Name(string startName,string endName)
            {
                _startName = startName;
                _endName = endName;

                return this;
            }

            public ListQueryItem_ValueRange Value(string startValue,string endValue)
            {
                _startValue = startValue;
                _endValue = endValue;

                return this;
            }

            public ListQueryItem_ValueRange Label(string label)
            {
                _label = label;

                return this;
            }

            public override MvcHtmlString Render()
            {
                ViewContext.Writer.WriteLine($"<div {_attrs}");
                ViewContext.Writer.WriteLine("<div class=\"weui-flex__item weui-flex-row\">");

                if (!string.IsNullOrWhiteSpace(_label))
                {
                    ViewContext.Writer.WriteLine($"<div class=\"weui-cells__title\">{_label}</div>");
                }

                ViewContext.Writer.WriteLine("<div class=\"weui-cells weui-cells__body\">");
                ViewContext.Writer.WriteLine("<div class=\"weui-cell__bd\">");

                ViewContext.Writer.WriteLine($"<input type=\"text\" name=\"{_startName}\" placeholder=\"{_startPlaceholder}\" class=\"weui-input startvalue\" value=\"{_startValue}\"/>-<input type=\"text\" name=\"{_endName}\" value=\"{_endValue}\" placeholder=\"{_endPlaceholder}\" class=\"weui-input endvalue\" />");

                ViewContext.Writer.WriteLine("</div> <!--end  cell__bd-->");
                ViewContext.Writer.WriteLine("</div> <!-- end cells__body -->");
                
                ViewContext.Writer.WriteLine("</div> <!-- end flex-row -->");
                ViewContext.Writer.WriteLine("</div> <!-- end querybox-value -->");

                return MvcHtmlString.Empty;
            }
        }
    }
}